﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL;

namespace WebLibrary
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BooksManager bm = new BooksManager();
                BooksView.DataSource = bm.getAllBooksTitleByAsc();

                ViewState["BooksView.Sorting.Title"] = "Asc";
                ViewState["BooksView.Sorting.Author"] = "Desc";

                BooksView.DataBind();
            }

        }

        protected void BooksView_Sorting(object sender, GridViewSortEventArgs e)
        {

            if (e.SortExpression == "Title")
            {
                if (ViewState["BooksView.Sorting.Title"].ToString() == "Desc")
                {
                    BooksManager bm = new BooksManager();
                    BooksView.DataSource = bm.getAllBooksTitleByAsc();

                    ViewState["BooksView.Sorting.Title"] = "Asc";
                }
                else
                {
                    BooksManager bm = new BooksManager();
                    BooksView.DataSource = bm.getAllBooksTitleByDesc();

                    ViewState["BooksView.Sorting.Title"] = "Desc";
                }
            }



            if (e.SortExpression == "Author")
            {
                if (ViewState["BooksView.Sorting.Author"].ToString() == "Desc")
                {
                    BooksManager bm = new BooksManager();
                    BooksView.DataSource = bm.getAllBooksAuthorByAsc();

                    ViewState["BooksView.Sorting.Author"] = "Asc";
                }
                else
                {
                    BooksManager bm = new BooksManager();
                    BooksView.DataSource = bm.getAllBooksAuthorByDesc();

                    ViewState["BooksView.Sorting.Author"] = "Desc";
                }
            }


            BooksView.DataBind();
        }

        protected void filterButton_Click(object sender, EventArgs e)
        {
            BooksManager bm = new BooksManager();
            BooksView.DataSource = bm.getAllAvailableBooks();

            ViewState["BooksView.Sorting.Title"] = "Asc";


            BooksView.DataBind();

        }

        protected void btnTaken_Click(object sender, EventArgs e)
        {
            BooksManager bm = new BooksManager();
            BooksView.DataSource = bm.getAllTakenBooks();

            ViewState["BooksView.Sorting.Title"] = "Asc";


            BooksView.DataBind();

        }

        protected void btnAll_Click(object sender, EventArgs e)
        {
            BooksManager bm = new BooksManager();
            BooksView.DataSource = bm.getAllBooksTitleByAsc();

            ViewState["BooksView.Sorting.Title"] = "Asc";

            BooksView.DataBind();
        }

       
    }
}